﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbSharp.Framework.CaseFile;
using DbSharp.Framework.Driver;

namespace DbSharp.Framework.Storage
{
    public interface IStorageFactory
    {
        /// <summary>
        /// to create one IRecordsetStorage service
        /// </summary>
        /// <param name="rsSection">ResultsetSection object, this object will tell the IScalarStorage service how to store data</param>
        /// <param name="dbTestPlugin">database plugin</param>
        /// <returns>IRecordsetStorage service</returns>
        IRecordsetStorage NewRcdSetStorage(ResultsetSection rsSection, ITestDataSource dbTestPlugin);
        
        /// <summary>
        /// to create one IScalarStorage service
        /// </summary>
        /// <param name="rsSection">ResultsetSection object, this object will tell the IScalarStorage service how to store data</param>
        /// <param name="dbTestPlugin">database plugin</param>
        /// <returns>IScalarStorage service</returns>
        IScalarStorage NewScalarStorage(ResultsetSection rsSection, ITestDataSource dbTestPlugin);
    }


    public static class StorageFactoryHelper
    {

        /// <summary>
        /// Get OutputSetting from one ResultsetSection
        /// </summary>
        /// <param name="rsSection"></param>
        /// <param name="settingAttr"></param>
        /// <param name="listFieldOutputName">
        /// key:UpperCase fieldName
        /// value: outputFieldName
        /// </param>
        public static void GetResultsetOutputSetting(
            ResultsetSection rsSection,
            out OutputSettingAttribute settingAttr,
            out Dictionary<string, string> listFieldOutputName,
            out Dictionary<string, string> listFieldNullPlaceholder)
        {
            settingAttr = new OutputSettingAttribute();
            listFieldOutputName = new Dictionary<string, string>();
            listFieldNullPlaceholder = new Dictionary<string, string>();

            if ((rsSection != null) && (rsSection.OutputSetting != null))
            {
                //get output attributes
                settingAttr.ExportRecordCount = rsSection.OutputSetting.ExportRecordCount;
                settingAttr.WrapStringByCDATA = rsSection.OutputSetting.WrapStringByCDATA;

                //handle output fields
                if (rsSection.OutputSetting.IndicateOutputAllFields)
                {
                    //output all fields by default
                    listFieldOutputName.Add("*", "*");
                }
                else
                {
                    foreach (var pair in rsSection.OutputSetting.GetFieldOutputNamePairs())
                    {
                        listFieldOutputName.Add(pair.Key, pair.Value);
                    }
                }

                //handle null place holder for each field
                foreach (var pair in rsSection.OutputSetting.GetFieldNullPlaceholderPairs())
                {
                    listFieldNullPlaceholder.Add(pair.Key, pair.Value);
                }
            }
        }
    }

}
